package com.erp.erp_ui.Seal;

import com.erp.erp_entitys.Seal.SealEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.OperationLog.IOperationLogService;
import com.erp.erp_servers.Seal.ISealService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @ClassName SealControl
 * @Description 印章控制类
 * @Author songjizhen
 * @Date 2020-06-30 21:13
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "seal")
 public class SealControl extends Base {
    @Autowired
    ISealService sealService;
    @Autowired
    IOperationLogService operationLogService;

    /**
     * 根据主键删除印章,物理删除,即不可恢复
     * @param id
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> delete(Integer id){
        try {
            int num = sealService.deleteByPrimaryKey(id);
            if(num>0){
                SealEntity sealEntity = sealService.selectByPrimaryKey(id);
                UserEntity user = (UserEntity) this.getBean("user");
                operationLogService.insertlog("删除",user.getCode(),sealEntity.getCompanyCode(),"删除了印章");
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 新增印章信息
     * @param
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert( String id,String img,String type,String companyCode){//@RequestBody SealEntity record
        try {
            int num=0;
            SealEntity record=new SealEntity();
            UserEntity user=(UserEntity)this.getBean("user");
            record.setCreateor(user.getCode());
            record.setCreatTime(new Date());
            record.setImg(img);
            record.setType(type);
            record.setCompanyCode(companyCode);
            if(id==""){
                 num = sealService.insertSelective(record);
            }else{
                num=sealService.updateByPrimaryKeySelective(record);
            }
            if(num>0){
                if(id=="") {
                    operationLogService.insertlog("新增", user.getCode(), companyCode, "新增了印章");
                }else{
                    operationLogService.insertlog("更新", user.getCode(), companyCode, "更新了印章");
                }
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 查询印章信息
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage",method = RequestMethod.POST)
    @ResponseBody

    public  R<List<SealEntity>> selectByPage(String CompanyCode,String pageNum,String pageSize){
        try {
            SealEntity record=new SealEntity();
                    record.setCompanyCode(CompanyCode);
            List<SealEntity> sealEntities = sealService.selectByExample(record);

            return R.build(sealEntities);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据所属公司查询印章
     * @param CompanyCode
     * @return
     */
    @RequestMapping(value = "selectByCompanyCode")
    @ResponseBody
    public   R<List<SealEntity>> selectByCompanyCode(String CompanyCode){
        try {
            SealEntity record=new SealEntity();
            record.setCompanyCode(CompanyCode);
            List<SealEntity> sealEntities = sealService.selectByExample(record);
            return R.build(sealEntities);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 更新印章信息
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody SealEntity record){
        try {
            int num = sealService.updateByExampleSelective(record);
            if(num>0){
                UserEntity user=(UserEntity)this.getBean("user");
                operationLogService.insertlog("更新", user.getCode(), record.getCompanyCode(), "更新了印章");
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }
}
